<template>
  <div :class="className" :style="{height:height,width:width}" id="eChart" />
</template>

<script>
import echarts from 'echarts'
import elementResizeDetectorMaker from 'element-resize-detector'
require('echarts/theme/macarons') // echarts theme
import resize from '@/components/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    const that = this
    var echart = document.getElementById('eChart')
    this.$nextTick(() => {
      this.initChart()
    })
    var erd = elementResizeDetectorMaker()
    erd.listenTo(echart, function() {
      that.$nextTick(function() {
        echarts.init(echart).resize()
      })
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
    //   console.log(chartData)
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          data: ['快递', '快运']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ['09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '快递',
            type: 'line',
            stack: '发货总量',
            itemStyle: {
              normal: {
                color: '#36a3f7',
                lineStyle: {
                  color: '#36a3f7',
                  width: 2
                },
                areaStyle: {
                  color: '#36a3f7'
                }
              }
            },
            data: chartData.expressData
          },
          {
            name: '快运',
            type: 'line',
            stack: '发货总量',
            itemStyle: {
              normal: {
                color: '#f4516c',
                lineStyle: {
                  color: '#f4516c',
                  width: 2
                },
                areaStyle: {
                  color: '#f4516c'
                }
              }
            },
            data: chartData.giggitData
          }
        ]
      })
    }
  }
}
</script>
